﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WR.BL.BE;
using System.Collections.Generic;
using WR.BL.BCC;

namespace WR.UI.MobWebApp
{
    public partial class material : System.Web.UI.Page
    {
        private UserBE _objUsr;

        private void checkLogin()
        {
            lblErrorMsg.InnerHtml = string.Empty;
            _objUsr = (UserBE)Session["UserBE"];
            if (_objUsr == null)
            {
                Response.Redirect("login.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.checkLogin();

            if (!Page.IsPostBack)
            {
                string strMatId = Page.Request.QueryString["mid"];
                MaterialBE objMat = null;

                if (!string.IsNullOrEmpty(strMatId))
                {
                    strMatId = strMatId.Trim();
                    List<MaterialBE> lstMateriales = (List<MaterialBE>)Session["lstMateriales"];
                    if (lstMateriales != null)
                    {
                        objMat = lstMateriales.Find(X => X.Codigo == strMatId);
                    }
                    else
                    {
                        _objUsr = (UserBE)Session["UserBE"];
                        objMat = MaterialBC.getInstance(Session.SessionID).read(_objUsr.Tienda, strMatId);
                    }
                    if (objMat != null)
                    {
                        lblCodigo.InnerHtml = objMat.Codigo;
                        lblDescripcion.InnerHtml = objMat.Descripcion;
                        lblEan.InnerHtml = objMat.Ean;
                        lblUMedida.InnerHtml = objMat.UMedida;
                        lblStock.InnerHtml = string.Format("{0:,0}", objMat.Stock);
                        lblPrecio.InnerHtml = string.Format("{0:,0.00}", objMat.Precio);
                        txtCantidad.Value = "1";

                        Session["objMat"] = objMat;
                    }
                }
            }
        }

        protected void lnkAdd_Click(object s, EventArgs e)
        {
            lblErrorMsg.InnerHtml = string.Empty;
            try
            {
                MaterialBE objMat = (MaterialBE)Session["objMat"];
                SalesOrderBE objSO = (SalesOrderBE)Session["SalesOrderBE"];
                if (objSO == null)
                {
                    objSO = new SalesOrderBE();
                }
                DetailBE objDet = new DetailBE();
                string maxPos = objSO.Detalle.Max(X => X.Pos);
                objDet.Pos = (Convert.ToInt16(maxPos) + 10).ToString();

                objDet.CodMat = lblCodigo.InnerHtml.Trim();
                objDet.Cant = Convert.ToDecimal(txtCantidad.Value.Trim());
                objDet.UMedida = lblUMedida.InnerHtml.Trim();
                if (objMat != null)
                {
                    objDet.Material = objMat;
                }
                objSO.Detalle.Add(objDet);

                Session["SalesOrderBE"] = objSO;
                lblOkMsg.InnerHtml = "Se añadió el material " + objDet.CodMat;
            }
            catch (Exception ex)
            {
                lblErrorMsg.InnerHtml = ex.Message;
            }
        }
    }
}
